package com.luoc.base;


import lombok.SneakyThrows;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * @Author: luoc
 * @Date: 2019/7/3 14:09
 */
public class test {

    @SneakyThrows
    public static void main(String[] args) throws IOException {
//        // 监听客户端连接
//        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//        // 绑定监听端口 设置为非阻塞模式
//        serverSocketChannel.socket().bind(new InetSocketAddress(InetAddress.getByName("127.0.0.1"),1010));
//        serverSocketChannel.configureBlocking(false);
//        // 创建Reactor线程,创建多路复用器并启动线程
//        Selector selector = Selector.open();
//       // new Thread().start();

      //  Parent p = new Son();
        test t = new test();
        t.joinDemo();
    }

    // InterruptDemo

    /**
     *  当interruptedException抛出时，会清楚中断标志位，也就是说
     *  isinterrupted会返回false
     */
    public void interruptDemo(){
        final  Thread sleepThread = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        final Thread busyThread = new Thread(){
            @Override
            public void run() {
                while (true) {
                    ;
                }
            }
        };

        sleepThread.start();
        busyThread.start();
        sleepThread.interrupt();
        busyThread.interrupt();
        while (sleepThread.isInterrupted()){
            ;
        }
        System.out.println("sleepThread is Interrupted:" + sleepThread.isInterrupted());
        System.out.println("busyThread isInterrupted:" + busyThread.isInterrupted());

    }

    // joinDemo
    public void joinDemo () throws InterruptedException {
        final Thread joinThread = new Thread(){
            @Override
            public void run() {
                for (int i=0;i<10;i++){
                    System.out.println(i);
                }
            }
        };
       // Thread.currentThread().join();
        joinThread.start();
        //joinThread.join();
        System.out.println("等待join方法执行完成");
    }

    public  void waitDemo (){
         final  Thread t1 = new Thread(){
                @Override
                public void run() {
                    System.out.println("waitThread");
                }
            };

         t1.start();
        try {
            t1.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }





}
